package com.east.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.east.dto.ActivityDTO;
import com.east.entity.Activity;
import com.east.entity.User;
import com.east.mapper.ActivityMapper;
import com.east.query.ActivityQuery;
import com.east.result.Result;
import com.east.service.ActivityService;
import com.east.utils.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Resource
    private ActivityMapper activityMapper;

    @Override
    public Result list(Integer currentPage, Integer pageSize) {
        Page<ActivityDTO> page = PageHelper.startPage(currentPage, pageSize);
        ActivityQuery activityQuery = new ActivityQuery();
        List<ActivityDTO> activityList = activityMapper.list(activityQuery);
        PageData<ActivityDTO> pageData = new PageData<>(currentPage, pageSize,
                Integer.valueOf(page.getTotal() + ""), page.getPages(), page.getResult());
        return Result.OK(pageData);
    }

    @Override
    public Result searchActivityList(Integer currentPage, Integer pageSize, ActivityQuery activityQuery) {
        Page<ActivityDTO> page = PageHelper.startPage(currentPage, pageSize);
        List<ActivityDTO> activityList = activityMapper.searchActivityList(activityQuery);
        PageData<ActivityDTO> pageData = new PageData<ActivityDTO>(currentPage, pageSize,
                Integer.valueOf(page.getTotal() + ""), page.getPages(), page.getResult());
        return Result.OK(pageData);
    }

    @Override
    public Result addActivity(ActivityDTO activityDTO) {
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        activityDTO.setCreateBy(loginUser.getId());
        activityDTO.setCreateTime(new Date());
        Activity activity = BeanUtil.copyProperties(activityDTO, Activity.class);
        int insert = activityMapper.insert(activity);
        return insert > 0 ? Result.OK() : Result.FAIL("添加失败");
    }

    @Override
    public Result activityInfo(Integer id) {
        if (id == null) {
            return Result.FAIL("未传入活动id");
        }
        ActivityDTO activityDTO = activityMapper.selectIncludeAcNameByPrimaryKey(id);
        return Result.OK(activityDTO);
    }

    @Override
    public Result update(ActivityDTO activityDTO) {
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Activity activity = BeanUtil.copyProperties(activityDTO, Activity.class);
        activity.setEditTime(new Date());
        activity.setEditBy(loginUser.getId());
        int i = activityMapper.updateByPrimaryKeySelective(activity);
        return i > 0 ? Result.OK() : Result.FAIL("更新失败");
    }

    @Override
    public Result deleteUserByIds(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return Result.FAIL("未提供要删除的数据");
        }
        int i = activityMapper.batchDeleteByIds(ids);
        return i > 0 ? Result.OK() : Result.FAIL("删除失败");
    }

    @Override
    public Result getOnGoingActivity() {
        List<ActivityDTO> activityDTOS = activityMapper.getOnGoingActivity();
        return Result.OK(activityDTOS);
    }
}
